package com.jiayuge.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiayuge.server.common.entity.RespBean;
import com.jiayuge.server.pojo.MenuRole;
import com.jiayuge.server.mapper.MenuRoleMapper;
import com.jiayuge.server.service.IMenuRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Jiayu.Yang
 * @since 2021-11-15
 */
@Service
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements IMenuRoleService {
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    /**
     * 功能: 更新角色菜单  |  更新角色菜单  |  数据库: 增 | 删 | 改 | 查
     * * @param rid
     *
     * @param mids
     * @return {@link RespBean}
     * @Date 2021/11/25
     * @author jiayu.Yang
     */
    @Override
    @Transactional
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        //删除角色下的所有菜单
        menuRoleMapper.delete(new QueryWrapper<MenuRole>().eq("rid", rid));
        //清空角色的菜单
        if (mids == null || mids.length == 0) {
            return RespBean.success("更新成功");
        }

        //批量更新角色菜单表
        Integer result = menuRoleMapper.insertRecord(rid, mids);
        if (result==mids.length){
            return RespBean.success("更新成功");
        }

        return RespBean.error("更新失败");
    }
}
